﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Threading.Tasks;
using Models.ClientModels;
using System.IO;

namespace Client
{
    public static class Cfg
    {
        public static ClientConfigModel Configs = new ClientConfigModel();
        public static string ConfigPath;
        public static string ServerAddr = "http://Ddns.261k.com/";
        //public static string ServerAddr = "http://localhost:52790/";

        #region 创建初始配置文件

        public static void InitNewConfigFile()
        {
            Configs.AutoRun = true;
            Configs.RunToTray = true;
            Configs.CloseToTray = true;
            Configs.Log = true;
            if (Configs.Accounts is null)
                Configs.Accounts = new List<string>();
            else
                Configs.Accounts.Clear();
            SaveConfig();
        }

        #endregion 创建初始配置文件

        public static void GetConfig()
        {
            if (!File.Exists(ConfigPath))
                InitNewConfigFile();
            XmlDocument xd = new XmlDocument();
            xd.Load(ConfigPath);
            //配置信息
            XmlNode root = xd.SelectSingleNode("ROOT");
            if (null == root)
            {
                InitNewConfigFile();
                xd.Load(ConfigPath);
                root = xd.SelectSingleNode("ROOT");
                if (root == null)
                    throw new Exception("读取配置文件失败");
            }
            XmlNode config = root.SelectSingleNode("Config");
            XmlNode accounts = root.SelectSingleNode("Account");
            string tmp = config.SelectSingleNode("AutoRun").InnerText;
            Configs.AutoRun = string.IsNullOrWhiteSpace(tmp) ? true : Convert.ToBoolean(tmp);
            tmp = config.SelectSingleNode("RunToTray").InnerText;
            Configs.RunToTray = string.IsNullOrWhiteSpace(tmp) ? true : Convert.ToBoolean(tmp);
            tmp = config.SelectSingleNode("CloseToTray").InnerText;
            Configs.CloseToTray = string.IsNullOrEmpty(tmp) ? true : Convert.ToBoolean(tmp);
            tmp = config.SelectSingleNode("Log").InnerText;
            Configs.Log = string.IsNullOrEmpty(tmp) ? false : Convert.ToBoolean(tmp);
            //账户列表
            if (Configs.Accounts is null)
                Configs.Accounts = new List<string>();
            else
                Configs.Accounts.Clear();
            if (accounts.HasChildNodes)
            {
                for (int i = 0; i < accounts.ChildNodes.Count; i++)
                {
                    var child = accounts.ChildNodes[i];
                    Configs.Accounts.Add(child.InnerText);
                }
            }
        }

        #region 保存配置

        public static void SaveConfig()
        {
            XmlDocument xDoc = new XmlDocument();
            XmlDeclaration xDec = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement root = xDoc.CreateElement("ROOT");
            XmlElement config = xDoc.CreateElement("Config");
            XmlElement account = xDoc.CreateElement("Account");
            XmlElement xl = xDoc.CreateElement("AutoRun");
            xl.InnerText = Configs.AutoRun.ToString();
            config.AppendChild(xl);
            xl = xDoc.CreateElement("RunToTray");
            xl.InnerText = Configs.RunToTray.ToString();
            config.AppendChild(xl);
            xl = xDoc.CreateElement("CloseToTray");
            xl.InnerText = Configs.CloseToTray.ToString();
            config.AppendChild(xl);
            xl = xDoc.CreateElement("Log");
            xl.InnerText = Configs.Log.ToString();
            config.AppendChild(xl);
            //账户
            for (int i = 0; i < Configs.Accounts.Count; i++)
            {
                xl = xDoc.CreateElement("AccId");
                xl.InnerText = Configs.Accounts[i];
                account.AppendChild(xl);
            }
            root.AppendChild(config);
            root.AppendChild(account);
            xDoc.AppendChild(xDec);
            xDoc.AppendChild(root);
            xDoc.Save(ConfigPath);
        }

        #endregion 保存配置
    }
}